package com.yang.dubbo.service;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.yang.dubbo.dao.*;
import com.yang.dubbo.model.*;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Component
public class TicketServiceImp implements TicktService{
    @Autowired
    private MemberDao memberDao;

    @Autowired
    private MealDao mealDao;

    @Autowired
    private ProjectDao projectDao;

    @Autowired
    private ProductDao productDao;

    @Autowired
    private ProjectTypeDao projectTypeDao;

    @Autowired
    private ProductTypeDao productTypeDao;

    //@HystrixCommand
    @Override
    public String getTicket() {
        return "DUBBO";
        //throw new RuntimeException("Hystri。。。。。。");
    }

    @Override
    public Map<String, Object> queryAll() {
        List<Member> mList = memberDao.findAll();

        Map<String, Object> msg = new HashMap<String, Object>();
        msg.put("massage", "查询成功");
        msg.put("data", mList);
        msg.put("code", 200);
        return msg;
    }

    @Override
    public Map<String, Object> insertAll() {
        ProductType dt1 = new ProductType(1, "DT1");
        ProductType dt2 = new ProductType(2, "DT2");
        productTypeDao.save(dt1);
        productTypeDao.save(dt2);

        ProjectType jt1 = new ProjectType(1, "JT1");
        ProjectType jt2 = new ProjectType(2, "JT2");
        projectTypeDao.save(jt1);
        projectTypeDao.save(jt2);

        Product d1 = new Product(1,"D1");
        Product d2 = new Product(2,"D2");
        d1.setType(dt2);
        d2.setType(dt1);
        productDao.save(d1);
        productDao.save(d2);

        Project j1 = new Project(1,"J1");
        Project j2 = new Project(2,"J2");
        j1.setType(jt2);
        j2.setType(jt1);
        projectDao.save(j1);
        projectDao.save(j2);

        Meal e1 = new Meal(1,"E1");
        Meal e2 = new Meal(2,"E2");
        Meal e3 = new Meal(3,"E3");
        e1.getProductList().add(d2);
        e2.getProductList().add(d1);
        e1.getProjectList().add(j2);
        e2.getProjectList().add(j1);
        e3.getProductList().add(d1);
        e3.getProjectList().add(j1);
        mealDao.save(e1);
        mealDao.save(e2);
        mealDao.save(e3);

        Member m1 = new Member(1,"M1");
        Member m2 = new Member(2,"M2");
        m1.getMeallist().add(e1);
        m1.getMeallist().add(e2);
        m2.getMeallist().add(e3);
        memberDao.save(m1);
        memberDao.save(m2);

        Map<String, Object> msg = new HashMap<String, Object>();
        msg.put("massage", "插入成功");
        msg.put("code", 200);
        return msg;
    }


}
